#!/bin/bash
# define usage function
usage(){
	echo "Usage: $0 <N> <p> <b> <number of tests>"
	exit 1
}
 

# invoke  usage
# call usage() function if filename not supplied
[ $# -ne 4 ] && usage

# set up the paramater file
echo "#define N $1" > params.h
echo "#define p $2" >> params.h
echo "#define b $3" >> params.h

echo "---------------------------------------------"
echo "Delegates are:"
if grep -q "smartDelegate (true)" pub.h
then echo "    smart"
else echo "    dumb"
fi
echo "Bodyguards are:"
if grep -q "smartGuard (true)" pub.h
then echo "    smart"
else echo "    dumb"
fi
echo "Accumulative counting:"
if grep -q "accumulativeCounting (true)" pub.h
then echo "    on"
else echo "    off"
fi
echo "---------------------------------------------"



WIN1=0
WIN2=0
LOSS1=0
LOSS2=0
LOSS3=0
UNKNOWN=0

for ((i=0 ; i<$4 ; i++))
do
		spin pub.pml > tmp
		if grep -q "PUB CRAWL WAS SUCCESSFUL" tmp 
		then
			WIN1=$((WIN1+1))
		elif grep -q "GUARD WAS INTOXICATED" tmp 
		then
			WIN2=$((WIN2+1))
		elif grep -q "DELEGATES GOT SPLIT" tmp 
		then
			LOSS1=$((LOSS1+1))
		elif grep -q "TOO MANY WENCHES" tmp 
		then
			LOSS2=$((LOSS2+1))
		elif grep -q "NO BEER WENCH" tmp
		then
			LOSS3=$((LOSS3+1))
		else 
			UNKNOWN=$((UNKNOWN+1))
		fi
done

rm tmp

total_wins=`expr $WIN1 + $WIN2`
total_loss=`expr $LOSS1 + $LOSS2`

echo "Total wins: $total_wins"
echo "Total losses: $total_loss"
echo "---------------------------------------------"
echo "Finished pub crawls: $WIN1"
echo "Intoxicated guards: $WIN2"
echo "Split delegates: $LOSS1"
echo "Double wenches: $LOSS2"
echo "No Wenches: $LOSS3"
echo "---------------------------------------------"
echo "Unknown cases (should be 0): $UNKNOWN"
echo "---------------------------------------------"

